package com.yuke.cloud.service.opc.service.impl;

import com.yuke.cloud.service.opc.entity.FastDFSFile;
import com.yuke.cloud.service.opc.service.OpcFileService;
import com.yuke.cloud.service.opc.utils.FastDFSClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;

/*
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yuke-mall </br> </p>
 * <p>com.yuke.cloud.service.opc.service.impl Description TODO </br> </p>
 * @Author wg
 * @Date   2020/6/13 14:23
 */
@Slf4j
@Service
public class OpcFileServiceImpl implements OpcFileService {

    @Override
    public String saveFiles(InputStream inputStream, String fileName, String ext) throws IOException {
        log.info("----- Enter saveFile() -----");
        String[] fileAbsolutePath={};
        byte[] file_buff = null;
        if(inputStream!=null){
            int len1 = inputStream.available();
            file_buff = new byte[len1];
            inputStream.read(file_buff);
        }
        inputStream.close();
        FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
        try {
            fileAbsolutePath = FastDFSClient.upload(file);  //upload to fastdfs
        } catch (Exception e) {
            log.error("upload file Exception!",e);
        }
        if (fileAbsolutePath==null) {
            log.error("upload file failed,please upload again!");
        }
        String path = fileAbsolutePath[0]+ "/"+fileAbsolutePath[1];
        log.info("----- Leave saveFile()，返回值为-----" + path);
        return path;
    }
}
